package com.wanlian.service.impl;

import java.util.List;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.AuthorityDao;
import com.wanlian.entity.Authority;
import com.wanlian.model.ResultModel;
import com.wanlian.service.AuthorityServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AuthorityServiceImpl implements AuthorityServices {

    @Autowired
    private AuthorityDao authorityDao;

    @Override
    public ResultModel doAdd(Authority authority) {
        ResultModel resultModel = new ResultModel(true);
        int parentId = authority.getParentid();
        if (parentId > 0) {
            Authority authoritydb = authorityDao.queryById(parentId);
            if (authoritydb == null) {
                resultModel.setMessage("添加失败，父级节点不存在");
                resultModel.setBresult(false);
                return resultModel;
            }
        }
     /*   if (authorityDao.queryByName(authority.getAuthorityname(),authority.getMethodorurl()) != null) {
            resultModel.setBresult(false);
            resultModel.setMessage("该名称或权限地址已存在");
            return resultModel;
        }*/
            authorityDao.add(authority);
        resultModel.setMessage("添加成功");
        return resultModel;

    }

    @Override
    public ResultModel doedit(Authority authority) {
        ResultModel resultModel = new ResultModel(true);
        Authority db_authority=authorityDao.queryById(authority.getAuthorityId());
        db_authority.setCreateid(authority.getCreateid());
        db_authority.setAuthorityname(authority.getAuthorityname());
        db_authority.setMethodorurl(authority.getMethodorurl());
        authorityDao.update(db_authority);
        resultModel.setMessage("修改成功");
        return resultModel;

    }

    @Override
    public ResultModel doDel(Authority authority) {
        ResultModel resultModel = new ResultModel(true);
        Authority authoritydb = authorityDao.queryById(authority.getAuthorityId());
        if (authoritydb == null) {
            resultModel.setMessage("该权限不存在");
            resultModel.setBresult(false);
            return resultModel;
        } else {
            List<Authority> lists = authorityDao.queryListByParentid(authority.getAuthorityId());
            if (lists.size() > 0) {
                resultModel.setMessage("该权限下有子权限，无法禁用");
                resultModel.setBresult(false);
                return resultModel;
            } else {
                authoritydb.setIsenable(false);
                authorityDao.update(authoritydb);
                resultModel.setMessage("删除成功");
                resultModel.setBresult(true);
                return resultModel;
            }
        }
    }


    @Override
    public ResultModel queryPageList(PageCondition pageCondition) {
        return authorityDao.queryPageList(pageCondition);
    }

    @Override
    public Authority queryByMethodUrl(String metnodUrl) {
        return authorityDao.queryByMethodUrl(metnodUrl);
    }
}
